// @ts-nocheck
import { Plugin, TransformResult } from 'vite'
import Utils from '../utils'

export default function vitePluginChangeMain(): Plugin {
  return {
    enforce: 'pre',
    name: 'vite:vite-plugin-change-main',
    transform(code: string, id: string): Promise<TransformResult> | TransformResult {
      let map = null
      if (id.includes('node_modules')) {
        return { code, map }
      }
      if (id.endsWith('main.js') || id.endsWith('main.ts')) {
        const s = Utils.getMagicString(code)
        s.prepend('import \'virtual:svg-icons-register\';\n')
        map = Utils.getsouceMap(s, code)
        code = s.toString()
      }
      return {
        code,
        map
      }
    }

  }
}
